#include "Spark/Net/Timer.h"

std::atomic<TimerID> Timer::timersCreated{0};

Timer::Timer(Task task, TimePoint when, SysTime::duration interval)
    : task_(std::move(task)),
      when_(when),
      interval_(interval),
      repeat_(interval_.count() > 0),
      id_(++timersCreated)
{
}

Timer::~Timer()
{
}

void Timer::run()
{
    task_();
}

void Timer::restart(TimePoint when)
{
    if (repeat_)
    {
        when_ = when + interval_;
    }
    else
    {
        when_ = TimePoint::Now();
    }
}
